package com.tuozixuan.api.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.repository.ProcessDefinitionQuery;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import com.tuozixuan.api.TestApi;

@RestController
public class TestApiImpl implements TestApi {

	@Autowired
	private RepositoryService repositoryService;

	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private TaskService taskService;

	@Autowired
	private HistoryService historyService;
	
    @Autowired
    private ProcessEngine engine;

	@Override
	public void deploy() {
		Deployment deploy = repositoryService.createDeployment().addClasspathResource("bpmn/testApply.bpmn").deploy();
		System.out.println(deploy.getId());
	}

	@Override
	public void start() {
		Map<String, Object> params = new HashMap<>();
		params.put("amount", 2001);
		ProcessInstance holiday = runtimeService.startProcessInstanceByKey("testApply", params);
		System.out.println(holiday.getProcessInstanceId());
	}
	
    /**
     * @Description: 流程任务查询
     * @Author: zhouhong
     * @Date: 2021/7/8
     */
    public void taskQuery() {
        List<Task> tasks = taskService.createTaskQuery()
                .processDefinitionKey("testApply")
                .list();
         for (Task task : tasks) {
            System.out.println(task.getAssignee());
            System.out.println(task.getId());
            System.out.println(task.getName());
            System.out.println(task.getTenantId());
        }
    }

    /**
    * @Description: 当前需要处理的任务
    * @Author: zhouhong
    * @Date: 2021/7/8
    */
    public List<HistoricTaskInstance> myTaskQuery() {
        List<HistoricTaskInstance> instances = engine.getHistoryService().createHistoricTaskInstanceQuery()
                .taskAssignee("lisi").unfinished().orderByHistoricActivityInstanceStartTime().asc().list();
        return instances;
    }

	@Override
	public void taskComplete() {
		// 目前zhangsan只有一个任务，业务中根据场景选择其他合适的方式
		Task task = taskService.createTaskQuery().taskAssignee("zhangsan").singleResult();
		Map<String, Object> params = new HashMap<>();
		params.put("approve2", "lisi");
		taskService.complete(task.getId(), params);
	}

	@Override
	public void queryDefine() {
		ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
		List<ProcessDefinition> definitions = query.processDefinitionKey("testApply").orderByProcessDefinitionVersion()
				.desc().list();
		for (ProcessDefinition definition : definitions) {
			System.out.println(definition.getDeploymentId());
			System.out.println(definition.getName());
			System.out.println(definition.getVersion());
			System.out.println(definition.getId());
			System.out.println(definition.getKey());
		}
	}

	/**
	 * 删除流程定义
	 */
	public void deleteDefine() {
		ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
		List<ProcessDefinition> definitions = processDefinitionQuery.processDefinitionKey("testApply")
				.orderByProcessDefinitionVersion().asc().list();
		ProcessDefinition processDefinition = definitions.get(0);
		if (processDefinition != null) {
			repositoryService.deleteDeployment(processDefinition.getDeploymentId(), true);
		}
	}

	/**
	 * 查询历史信息
	 */
	public void queryHistory() {
		List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery().finished()
				.orderByHistoricActivityInstanceEndTime().asc().list();
		for (HistoricActivityInstance instance : list) {
			System.out.println(instance.getActivityId());
			System.out.println(instance.getProcessDefinitionKey());
			System.out.println(instance.getAssignee());
			System.out.println(instance.getStartTime());
			System.out.println(instance.getEndTime());
			System.out.println("=============================");
		}
	}

	/**
	 * 启动一个流程实例，并且添加一个业务key 业务key 可以在 act_ru_execution 中看到
	 */
	public void startProcInstAddBusinessKey() {
		ProcessInstance apply = runtimeService.startProcessInstanceByKey("testApply", "aaaa-scsc-89uc");
		System.out.println(apply.getBusinessKey());
	}
}
